/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.admin.task;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yami.shop.bean.enums.ProfitBackStatus;
import com.yami.shop.bean.enums.ProfitTypeConst;
import com.yami.shop.bean.event.DealProfitEvent;
import com.yami.shop.bean.model.Order;
import com.yami.shop.bean.model.ProfitBackInfo;
import com.yami.shop.service.OrderService;
import com.yami.shop.service.ProfitBackInfoService;
import com.yami.shop.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component("profitTask")
public class ProfitTask {


    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private ProfitBackInfoService profitBackInfoService;

    @Autowired
    private ApplicationEventPublisher eventPublisher;


    /**
     * 处理7天后的分润
     */
    public void dealProfit() {
        // DateTime confirmTime = DateUtil.offsetDay(new Date(), -7);
        DateTime confirmTime = DateUtil.offsetMinute(new Date(), -1);
        /**
         * 已完成 且过去了7天的订单
         */
        List<Order> list = orderService.list(Wrappers.lambdaQuery(new Order())
                .eq(Order::getStatus, 5)
                .le(Order::getFinallyTime, confirmTime));
        List<String> orderNumbers = list.stream().map(Order::getOrderNumber).collect(Collectors.toList());

        if(orderNumbers.size()!=0){
            log.info("发现待处理分润订单"+orderNumbers);

            /**
             * 把这些订单的分润都筛选出来
             */
            List<ProfitBackInfo> ProfitBackInfos = profitBackInfoService.list(Wrappers.lambdaQuery(new ProfitBackInfo())
                    .in(ProfitBackInfo::getOrderNumber, orderNumbers)
                    .eq(ProfitBackInfo::getProfitStatus, ProfitBackStatus.ON_THE_WAY.getValue()));

            //处理list的所有分润
            // eventPublisher.publishEvent(new DealProfitEvent(ProfitBackInfos));

            for (ProfitBackInfo p : ProfitBackInfos) {
                String toUserId = p.getToUserId();
                Double amount = p.getAmount();
                Integer profitType = p.getProfitType();
                ProfitTypeConst profitTypeConst = getProfitTypeConst(profitType);

                userService.addScoreByUid(toUserId,amount, profitTypeConst,profitTypeConst.getMsg());
                p.setProfitStatus(ProfitBackStatus.COMPLETE.getValue());
                profitBackInfoService.updateById(p);
            }
        }else{

            log.info("没有要处理的分润");
        }
    }

    public ProfitTypeConst getProfitTypeConst(Integer type){
        for(ProfitTypeConst p:ProfitTypeConst.values()){
            if (p.getValue() == type){
                return p;
            }
        }
        return null;
    }
}
